package com.act;

import java.util.ArrayList;

import org.apache.http.Header;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bean.CallResult;
import com.common.Constant;
import com.common.TextHttpResponseHandler;
import com.control.UserControl;
import com.xike.kyt.R;
import com.xike.util.MyUtil;

public class CallAct extends BaseAct implements OnClickListener {

	TextView tv_headTitle;

	LinearLayout layout_step1, layout_step2;
	TextView tv_userName, tv_userMobile;
	TextView tv_toUserName, tv_toUserMobile;

	LinearLayout layout_startCall;
	TextView tv_serviceNum;
	Button btn_back;

	long user_id;

	int step = 1;

	String strToUserName, strToUserMobile;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_call);

		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			if (bundle.containsKey("toUserName")) {
				strToUserName = bundle.getString("toUserName");
			}
			if (bundle.containsKey("toUserMobile")) {
				strToUserMobile = bundle.getString("toUserMobile");
			}

			if (bundle.containsKey("toUserId")) {
				user_id = bundle.getLong("toUserId");
			}

		}

		Log.e("", "toUserMobile:" + strToUserMobile + ",user_id:" + user_id);

		initViews();
	}

	@Override
	protected void initViews() {

		findViewById(R.id.headTitle_tv_back).setOnClickListener(this);
		tv_headTitle = (TextView) findViewById(R.id.headTitle_tvTitle);

		layout_step1 = (LinearLayout) findViewById(R.id.call_layout_step1);
		layout_step2 = (LinearLayout) findViewById(R.id.call_layout_step2);

		tv_userName = (TextView) findViewById(R.id.call_tv_userName);
		tv_userMobile = (TextView) findViewById(R.id.call_tv_userMobile);
		tv_toUserName = (TextView) findViewById(R.id.call_tv_toUserName);
		tv_toUserMobile = (TextView) findViewById(R.id.call_tv_toUserMobile);
		tv_serviceNum = (TextView) findViewById(R.id.call_tv_serviceNum);

		MyUtil.setTextViewText(tv_toUserName, strToUserName, 0, "未知用户");
		MyUtil.setTextViewText(tv_toUserMobile, strToUserMobile, View.INVISIBLE, "");

		MyUtil.setTextViewText(tv_userName, UserControl.getLoginUserName(), 0, "我");
		MyUtil.setTextViewText(tv_userMobile, UserControl.getLoginUserBindMobile(), View.INVISIBLE, "");

		btn_back = (Button) findViewById(R.id.call_btn_back);
		layout_startCall = (LinearLayout) findViewById(R.id.call_layout_startCall);

		btn_back.setOnClickListener(this);
		layout_startCall.setOnClickListener(this);

	}

	@Override
	protected void updateViews() {

	}

	private void serviceStartCall() {
		ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("user_id", String.valueOf(user_id)));

		client.post(Constant.SERVER_URL_CALL, MyUtil.getPostRequestParams(params), new TextHttpResponseHandler(this) {

			@Override
			public void onSuccess(int statusCode, Header[] headers, String responseString) {
				super.onSuccess(statusCode, headers, responseString);
				CallResult result = gson.fromJson(responseString, CallResult.class);
				if (result != null) {
					dealRequestResult(result);
				} else {
					mToast.showToast(CallAct.this, getResources().getString(R.string.net_error));
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
				mToast.showToast(CallAct.this, getResources().getString(R.string.net_error));
				super.onFailure(statusCode, headers, responseString, throwable);
			}
		});

	}

	private void dealRequestResult(CallResult result) {
		if (result.isReturnTrue()) {
			layout_step1.setVisibility(View.GONE);
			layout_step2.setVisibility(View.VISIBLE);
			MyUtil.setTextViewText(tv_serviceNum, result.getService_tel(), 0, "系统电话");
		} else {
			if (result.getError_code().equals("20140")) {
				UserControl.setIsPayUser(false);
			}
			mToast.showToast(this, result.getError_description());
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.call_btn_back:
			if (step == 2) {
				layout_step2.setVisibility(View.GONE);
				layout_step1.setVisibility(View.VISIBLE);
				tv_headTitle.setText("免费匿名电话");
			} else {
				finish();
			}
			break;
		case R.id.headTitle_tv_back:
			if (step == 2) {
				layout_step2.setVisibility(View.GONE);
				layout_step1.setVisibility(View.VISIBLE);
				tv_headTitle.setText("免费匿名电话");
				step = 1;
			} else {
				finish();
			}
			break;

		case R.id.call_layout_startCall:
			step = 2;
			tv_headTitle.setText("即将来电");
			serviceStartCall();
			break;

		default:
			break;
		}
	}

}
